﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Application.Admin.Article
{
    public class OpenArticleService : IOpenArticleService
    {
        Application.Admin.Article.IArticleInfoClassService infoClaService;

        Application.Admin.Article.IArticleService articleService;

        Application.Admin.Article.IArticlePicService articlePicService;

        Application.Admin.Sight.ISightPointService sightService;

        Application.Admin.Article.IArticleSightService articleSightService;

        Application.Admin.Pic.IOpenPicService openPicService;

        public OpenArticleService(Application.Admin.Article.IArticleInfoClassService infoCla,
            Application.Admin.Article.IArticleService article,
            Application.Admin.Article.IArticlePicService articlePic,
            Application.Admin.Sight.ISightPointService sight,
            Application.Admin.Article.IArticleSightService articleSight,
            Application.Admin.Pic.IOpenPicService openPic)
        {
            if (infoCla == null)
            {
                throw new ArgumentNullException("infoClaService is null");
            }
            if (article == null)
            {
                throw new ArgumentNullException("articleService is null");
            }
            if (articlePic == null)
            {
                throw new ArgumentNullException("articleService is null");
            }
            if (sight == null)
            {
                throw new ArgumentNullException("sightService is null");
            }
            if (articleSight == null)
            {
                throw new ArgumentNullException("articleSightService is null");
            }
            if (openPic == null)
            {
                throw new ArgumentNullException("openPicService is null");
            }
            infoClaService = infoCla;
            articleService = article;
            articlePicService = articlePic;
            sightService = sight;
            articleSightService = articleSight;
            openPicService = openPic;
        }

        public List<Application.Admin.Dto.OpenArticleDto> GetOpenArticleByClassId(int cla)
        {
            var articleIdList = infoClaService.GetList()
                .Where(e => e.ClassId == cla)
                .Select(e => e.ArticleId);
            var data = this.GetOpenArticleByIdList(articleIdList);
            return data;
        }

        public List<Application.Admin.Dto.OpenArticleDto> GetOpenActivity()
        {
            var data = this.GetOpenArticleByClassId(1);
            return data;
        }

        public List<Application.Admin.Dto.OpenArticleDto> GetOpenDiscount()
        {
            var data = this.GetOpenArticleByClassId(2);
            return data;
        }

        public List<Application.Admin.Dto.OpenArticleDto> GetOpenHoliday()
        {
            var data = this.GetOpenArticleByClassId(3);
            return data;
        }

        public Application.Admin.Dto.OpenPicDto GetOpenAtriclePicByArticleId(int id)
        {
            var picId = articlePicService.GetList().Where(e => e.ArticleId == id)
                .OrderByDescending(e => e.Id)
                .ThenByDescending(e => e.AddTime).Select(e => e.PicInfoId).FirstOrDefault();
            var data = openPicService.GetOpenPicById(picId);
            return data;
        }

        public List<Application.Admin.Dto.OpenArticleDto> GetOpenArticleByIdList(IQueryable<int> idList)
        {
            var article = articleService.GetList().Where(e => idList.Contains(e.Id))
                   .Where(e => e.State == true)
                   .Where(e => e.ExpiredDateTime > System.DateTime.Now)
                   .OrderByDescending(d => d.AddTime);
            List<Application.Admin.Dto.OpenArticleDto> data = null;
            if (article.Any())
            {
                data = new List<Application.Admin.Dto.OpenArticleDto>();
                Application.Admin.Dto.OpenArticleDto temp = null;
                foreach (var item in article)
                {
                    temp = new Dto.OpenArticleDto();
                    temp.ArticlePic = this.GetOpenAtriclePicByArticleId(item.Id);
                    temp.CustomDateTime = item.CustomDateTime;
                    temp.Id = item.Id;
                    temp.Remark = item.Remark;
                    temp.SubTitle = item.SubTitle;
                    temp.Title = item.Title;
                    temp.Url = "/open/article/preview/" + item.Id;
                    var sightModel = (from arSight in articleSightService.GetList()
                                      join sight in sightService.GetList() on arSight.SightId equals sight.Id
                                      where arSight.ArticleId == item.Id
                                      select sight).FirstOrDefault();
                    if (sightModel != null)
                    {
                        temp.SightId = sightModel.InternalId;
                        temp.SightLat = sightModel.Lat;
                        temp.SightLon = sightModel.Lon;
                        temp.SightName = string.IsNullOrEmpty(sightModel.Name) ? "" : sightModel.Name;
                    }
                    else
                    {
                        temp.SightName = "";
                    }
                    data.Add(temp);
                }
            }
            return data;
        }

        public string GetOpenArticleContentById(int id)
        {
            var res = "";
            var data = articleService.Get(id);
            if (data != null)
            {
                data.ViewCount += 1;
                res = data.Content;
                articleService.Modify(data, null);
            }
            return res;
        }
    }
}
